home *** CD-ROM | disk | FTP | other *** search
/ Action Games (2008) / akcnihry1.iso / AT-Robots 2.10 / ATR2FUNC.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-06-22  |  13.7 KB  |  664 lines

  1. unit atr2func;
  2.  
  3. (*
  4. Copyright (c) 1999, Ed T. Toton III. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9.  
  10.    Redistributions of source code must retain the above copyright notice,
  11.    this list of conditions and the following disclaimer.
  12.  
  13.    Redistributions in binary form must reproduce the above copyright notice, 
  14.    this list of conditions and the following disclaimer in the documentation
  15.    and/or other materials provided with the distribution.
  16.  
  17.    All advertising materials mentioning features or use of this software
  18.    must display the following acknowledgement:
  19.  
  20.         This product includes software developed by Ed T. Toton III &
  21.         NecroBones Enterprises.
  22.  
  23.    No modified or derivative copies or software may be distributed in the
  24.    guise of official or original releases/versions of this software. Such
  25.    works must contain acknowledgement that it is modified from the original.
  26.  
  27.    Neither the name of the author nor the name of the business or
  28.    contributers may be used to endorse or promote products derived
  29.    from this software without specific prior written permission.
  30.  
  31. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 
  32. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  33. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
  35. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  38. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  40. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *)
  42.  
  43. Interface
  44.  
  45. uses dos, crt, filelib, myfile, graph;
  46.  
  47. var
  48.  delay_per_sec:longint;
  49.  registered,graphix,sound_on:boolean;
  50.  reg_name:string;
  51.  reg_num:word;
  52.  sint,cost:array[0..255] of real;
  53.  
  54. procedure textxy(x,y:integer; s:string);
  55. procedure coltextxy(x,y:integer; s:string; c:byte);
  56. function hexnum(num:byte):char;
  57. function hexb(num:byte):string;
  58. function hex(num:word):string;
  59. function valuer(i:string):real;
  60. function value(i:string):longint;
  61. function cstrr(i:real):string;
  62. function cstr(i:longint):string;
  63. function zero_pad(n,l:longint):string;
  64. function zero_pads(s:string; l:longint):string;
  65. Function addfront(b:string;l:integer): string;
  66. Function addrear(b:string;l:integer): string;
  67. Function ucase(s:string):string;
  68. Function lcase(s:string):string;
  69. Function space(i:byte):string;
  70. Function repchar(c:char; i:byte):string;
  71. function ltrim(s1:string):string;
  72. function rtrim(s1:string):string;
  73. function btrim(s1:string):string;
  74. function lstr(s1:string; l:integer):string;
  75. function rstr(s1:string; l:integer):string;
  76. Procedure FlushKey;
  77. procedure calibrate_timing;
  78. procedure time_delay(n:integer); {n=milliseconds}
  79. procedure check_registration;
  80. function rol(n,k:integer):integer;
  81. function ror(n,k:integer):integer;
  82. function sal(n,k:integer):integer;
  83. function sar(n,k:integer):integer;
  84. procedure viewport(x1,y1,x2,y2:integer);
  85. procedure main_viewport;
  86. procedure make_tables;
  87. function robot_color(n:integer):integer;
  88. procedure box(x1,y1,x2,y2:integer);
  89. procedure hole(x1,y1,x2,y2:integer);
  90. procedure chirp;
  91. procedure click;
  92. function hex2int(s:String):integer;
  93. function str2int(s:String):integer;
  94. function distance(x1,y1,x2,y2:real):real;
  95. function find_angle(xx,yy,tx,ty:real):real;
  96. function find_anglei(xx,yy,tx,ty:real):integer;
  97. {FIFI}
  98. function bin(n:integer):string;
  99. function decimal(num,length:integer):string;
  100. {/FIFI}
  101.  
  102.  
  103. Implementation
  104.  
  105. procedure textxy(x,y:integer; s:string);
  106. begin
  107.  setfillstyle(1,0);
  108.  bar(x,y,x+length(s)*8,y+7);
  109.  outtextxy(x,y,s);
  110. end;
  111.  
  112. procedure coltextxy(x,y:integer; s:string; c:byte);
  113. begin
  114.  setcolor(c);
  115.  textxy(x,y,s);
  116. end;
  117.  
  118. function hexnum(num:byte):char;
  119. begin
  120.  case num of
  121.   0 : hexnum:='0';
  122.   1 : hexnum:='1';
  123.   2 : hexnum:='2';
  124.   3 : hexnum:='3';
  125.   4 : hexnum:='4';
  126.   5 : hexnum:='5';
  127.   6 : hexnum:='6';
  128.   7 : hexnum:='7';
  129.   8 : hexnum:='8';
  130.   9 : hexnum:='9';
  131.  10 : hexnum:='A';
  132.  11 : hexnum:='B';
  133.  12 : hexnum:='C';
  134.  13 : hexnum:='D';
  135.  14 : hexnum:='E';
  136.  15 : hexnum:='F';
  137.  else hexnum:='X';
  138.  end; {case}
  139. end;
  140.  
  141. function hexb(num:byte):string;
  142. begin
  143.  hexb:=hexnum(num shr 4)+hexnum(num and 15);
  144. end;
  145.  
  146. function hex(num:word):string;
  147. begin
  148.  hex:=hexb(num shr 8)+hexb(num and 255);
  149. end;
  150.  
  151.  
  152. function valuer(i:string):real;
  153. var
  154.  s:real;
  155.  n:integer;
  156. begin
  157.  val(i,s,n);
  158.  if (n>0) then s:=0;
  159.  valuer:=s;
  160. end;
  161.  
  162. function value(i:string):longint;
  163. var
  164.  s:longint;
  165.  n:integer;
  166. begin
  167.  val(i,s,n);
  168.  if (n>0) then s:=0;
  169.  value:=s;
  170. end;
  171.  
  172. function cstrr(i:real):string;
  173. var
  174.  s1:string[255];
  175. begin
  176.  str(i,s1);
  177.  cstrr:=s1;
  178. end;
  179.  
  180. function cstr(i:longint):string;
  181. var
  182.  s1:string[255];
  183. begin
  184.  str(i,s1);
  185.  cstr:=s1;
  186. end;
  187.  
  188. function zero_pad(n,l:longint):string;
  189. var
  190.  s:string;
  191. begin
  192.  s:=cstr(n);
  193.  while length(s)<l do
  194.   s:='0'+s;
  195.  zero_pad:=s;
  196. end;
  197.  
  198. function zero_pads(s:string;l:longint):string;
  199. var
  200.  s1:string;
  201. begin
  202.  s1:=s;
  203.  while length(s1)<l do
  204.   s1:='0'+s1;
  205.  zero_pads:=s1;
  206. end;
  207.  
  208.  
  209. Function addfront(b:string;l:integer): string;
  210. Begin
  211.   while length(b)< l do
  212.     b := ' '+b;
  213.   addfront := b;
  214. End;
  215.  
  216. Function addrear(b:string;l:integer): string;
  217. Begin
  218.   while length(b)< l do
  219.     b := b+' ';
  220.   addrear := b;
  221. End;
  222.  
  223. Function ucase(s:string):string;
  224. var
  225.  i:integer;
  226. begin
  227.  if length(s)>=1 then
  228.   begin
  229.    for i:=1 to length(s) do
  230.     s[i]:=upcase(s[i]);
  231.   end;
  232.  ucase:=s;
  233. end;
  234.  
  235. Function lcase(s:string):string;
  236. var
  237.  i:integer;
  238. begin
  239.  if length(s)>=1 then
  240.   begin
  241.    for i:=1 to length(s) do
  242.     if (ord(s[i])>=65) and (ord(s[i])<=90) then s[i]:=chr(ord(s[i])+32);
  243.   end;
  244.  lcase:=s;
  245. end;
  246.  
  247.  
  248. Function space(i:byte):string;
  249. var
  250.  s:string[255];
  251.  k:integer;
  252. begin
  253.  s:='';
  254.  if i>0 then for k:=1 to i do s:=s+' ';
  255.  space:=s;
  256. end;
  257.  
  258. Function repchar(c:char; i:byte):string;
  259. var
  260.  s:string[255];
  261.  k:integer;
  262. begin
  263.  s:='';
  264.  if i>0 then
  265.   for k:=1 to i do
  266.    begin
  267.     s:=s+c;
  268.    end;
  269.  repchar:=s;
  270. end;
  271.  
  272.  
  273. function ltrim(s1:string):string;
  274. var
  275.  i:integer;
  276. begin
  277.  while (length(s1)>0) and ((copy(s1,1,1)=' ') or (copy(s1,1,1)=#8) or (copy(s1,1,1)=#9)) do
  278.   begin
  279.    s1:=copy(s1,2,length(s1)-1);
  280.   end;
  281.  ltrim:=s1;
  282. end;
  283.  
  284. function rtrim(s1:string):string;
  285. var
  286.  i:integer;
  287. begin
  288.  while (length(s1)>0) and ((copy(s1,length(s1),1)=' ') or (copy(s1,length(s1),1)=#8)
  289.          or (copy(s1,length(s1),1)=#9)) do
  290.   begin
  291.    s1:=copy(s1,1,length(s1)-1);
  292.   end;
  293.  rtrim:=s1;
  294. end;
  295.  
  296. function btrim(s1:string):string;
  297. begin
  298.  btrim:=ltrim(rtrim(s1));
  299. end;
  300.  
  301. function lstr(s1:string; l:integer):string;
  302. begin
  303.  if length(s1)<=l then lstr:=s1
  304.  else lstr:=copy(s1,1,l);
  305. end;
  306.  
  307. function rstr(s1:string; l:integer):string;
  308. begin
  309.  if length(s1)<=l then rstr:=s1
  310.  else rstr:=copy(s1,length(s1)-l+1,l);
  311. end;
  312.  
  313. Procedure FlushKey;              { Clears any key strokes in the key-  }
  314.                                  { board buffer so a couple of key     }
  315. var                              { presses don't race you through program. }
  316.   Regs : Registers;
  317.  
  318. begin
  319.   Regs.AH := $01;  { AH=1: Check for keystroke }
  320.   Intr($16,regs); { Interupt $16: Keyboard services}
  321.   IF (regs.Flags and $0040) = 0 then { if chars in buffer }
  322.     REPEAT
  323.       Regs.AH := 0;
  324.       Intr($16,Regs);
  325.       Regs.AH := $01;
  326.       Intr($16,Regs);
  327.     Until (regs.flags and $0040) <> 0;
  328. end;
  329.  
  330.  
  331. procedure calibrate_timing;
  332. var
  333.  i,k:longint;
  334. begin
  335.  delay_per_sec:=0;
  336.  k:=mem[0:$46C];
  337.  repeat until k<>mem[0:$46C];
  338.  k:=mem[0:$46C];
  339.  repeat delay(1); inc(delay_per_sec); until k<>mem[0:$46C];
  340.  delay_per_sec:=round(delay_per_sec*18.2);
  341. end;
  342.  
  343. procedure time_delay(n:integer); {n=milliseconds}
  344. var
  345.  i,l:longint;
  346. begin
  347.  if delay_per_sec=0 then calibrate_timing;
  348.  l:=round(n/1000*delay_per_sec);
  349.  for i:=1 to l do delay(1);
  350. end;
  351.  
  352. procedure check_registration;
  353. var
  354.  w:word;
  355.  i:integer;
  356.  f:text;
  357.  s:String;
  358. begin
  359.  registered:=false;
  360.  if exist('ATR2.REG') then
  361.   begin
  362.    assign(f,'ATR2.REG');
  363.    reset(f);
  364.    readln(f,reg_name);
  365.    readln(f,reg_num);
  366.    close(f);
  367.    w:=0; s:=btrim(ucase(reg_name));
  368.    for i:=1 to length(s) do
  369.      inc(w,ord(s[i]));
  370.    w:=w xor $5AA5;
  371.    if w=reg_num then registered:=true;
  372.   end;
  373. end;
  374.  
  375.  
  376. function rol(n,k:integer):integer;
  377. begin
  378.  asm
  379.   cld
  380.   mov   cx,     k
  381.   rep   rol     n,      1
  382.  end;
  383.  rol:=n;
  384. end;
  385.  
  386. function ror(n,k:integer):integer;
  387. begin
  388.  asm
  389.   cld
  390.   mov   cx,     k
  391.   rep   ror     n,      1
  392.  end;
  393.  ror:=n;
  394. end;
  395.  
  396. function sal(n,k:integer):integer;
  397. begin
  398.  asm
  399.   cld
  400.   mov   cx,     k
  401. @1:
  402.   sal   n,      1
  403.   loop  @1
  404.  end;
  405.  sal:=n;
  406. end;
  407.  
  408. function sar(n,k:integer):integer;
  409. begin
  410.  asm
  411.   cld
  412.   mov   cx,     k
  413. @1:
  414.   sar   n,      1
  415.   loop  @1
  416.  end;
  417.  sar:=n;
  418. end;
  419.  
  420. procedure viewport(x1,y1,x2,y2:integer);
  421. begin
  422.  if not graphix then exit;
  423.  setviewport(x1,y1,x2,y2,true);
  424. end;
  425.  
  426. procedure main_viewport;
  427. begin
  428.  viewport(5,5,474,474); {470x470}
  429. end;
  430.  
  431. procedure make_tables;
  432. var
  433.  i,j,k:integer;
  434. begin
  435.  for i:=0 to 255 do
  436.   begin
  437.    sint[i]:=sin(i/128*pi);
  438.    cost[i]:=cos(i/128*pi);
  439.   end;
  440. end;
  441.  
  442. function robot_color(n:integer):integer;
  443. var
  444.  k:integer;
  445. begin
  446.  k:=7;
  447.  case n mod 14 of
  448.   0:k:=10;
  449.   1:k:=12;
  450.   2:k:=09;
  451.   3:k:=11;
  452.   4:k:=13;
  453.   5:k:=14;
  454.   6:k:=7;
  455.   7:k:=6;
  456.   8:k:=2;
  457.   9:k:=4;
  458.   10:k:=1;
  459.   11:k:=3;
  460.   12:k:=5;
  461.   13:k:=15;
  462.   else k:=15;
  463.  end;
  464.  robot_color:=k;
  465. end;
  466.  
  467.  
  468. procedure box(x1,y1,x2,y2:integer);
  469. var
  470.  i:integer;
  471. begin
  472.  if not graphix then exit;
  473.  if x2<x1 then begin i:=x1; x1:=x2; x2:=i; end;
  474.  if y2<y1 then begin i:=y1; y1:=y2; y2:=i; end;
  475.  setfillstyle(1,7);
  476.  setcolor(7);
  477.  bar(x1,y1,x2,y2);
  478.  setcolor(15);
  479.  line(x1,y1,x2-1,y1);
  480.  line(x1,y1,x1,y2-1);
  481.  setcolor(8);
  482.  line(x1+1,y2,x2,y2);
  483.  line(x2,y1+1,x2,y2);
  484. end;
  485.  
  486. procedure hole(x1,y1,x2,y2:integer);
  487. var
  488.  i:integer;
  489. begin
  490.  if not graphix then exit;
  491.  if x2<x1 then begin i:=x1; x1:=x2; x2:=i; end;
  492.  if y2<y1 then begin i:=y1; y1:=y2; y2:=i; end;
  493.  setfillstyle(1,0);
  494.  setcolor(0);
  495.  bar(x1,y1,x2,y2);
  496.  setcolor(8);
  497.  line(x1,y1,x2-1,y1);
  498.  line(x1,y1,x1,y2-1);
  499.  setcolor(15);
  500.  line(x1+1,y2,x2,y2);
  501.  line(x2,y1+1,x2,y2);
  502.  putpixel(x1,y2,7);
  503.  putpixel(x2,y1,7);
  504. end;
  505.  
  506. procedure chirp;
  507. begin
  508.  if (not sound_on) or (not graphix) then exit;
  509.  sound(250);
  510.  delay(10);
  511.  sound(1000);
  512.  delay(10);
  513.  sound(500);
  514.  delay(10);
  515.  sound(1000);
  516.  delay(10);
  517.  nosound;
  518. end;
  519.  
  520. procedure click;
  521. begin
  522.  if (not sound_on) or (not graphix) then exit;
  523.  sound(250);
  524.  delay(3);
  525.  sound(1000);
  526.  delay(3);
  527.  sound(500);
  528.  delay(3);
  529.  nosound;
  530. end;
  531.  
  532. function hex2int(s:String):integer;
  533. var
  534.  w:word;
  535.  i,j:integer;
  536. begin
  537.  i:=0; w:=0;
  538.  while i<length(s) do
  539.   begin
  540.    inc(i);
  541.    case s[i] of
  542.     '0':begin w:=(w shl 4) or $0; end;
  543.     '1':begin w:=(w shl 4) or $1; end;
  544.     '2':begin w:=(w shl 4) or $2; end;
  545.     '3':begin w:=(w shl 4) or $3; end;
  546.     '4':begin w:=(w shl 4) or $4; end;
  547.     '5':begin w:=(w shl 4) or $5; end;
  548.     '6':begin w:=(w shl 4) or $6; end;
  549.     '7':begin w:=(w shl 4) or $7; end;
  550.     '8':begin w:=(w shl 4) or $8; end;
  551.     '9':begin w:=(w shl 4) or $9; end;
  552.     'A':begin w:=(w shl 4) or $A; end;
  553.     'B':begin w:=(w shl 4) or $B; end;
  554.     'C':begin w:=(w shl 4) or $C; end;
  555.     'D':begin w:=(w shl 4) or $D; end;
  556.     'E':begin w:=(w shl 4) or $E; end;
  557.     'F':begin w:=(w shl 4) or $F; end;
  558.     else i:=length(s);
  559.    end;
  560.   end;
  561.  hex2int:=w;
  562. end;
  563.  
  564. function str2int(s:string):integer;
  565. var
  566.  i,j,k:longint;
  567.  neg:boolean;
  568. begin
  569.  neg:=false;
  570.  s:=btrim(ucase(s));
  571.  if s='' then k:=0 else
  572.   begin
  573.    if s[1]='-' then begin neg:=true; s:=rstr(s,length(s)-1); end;
  574.    k:=0;
  575.    if lstr(s,2)='0X' then
  576.      k:=hex2int(rstr(s,length(s)-2))
  577.    else if rstr(s,1)='H' then
  578.      k:=hex2int(lstr(s,length(s)-1))
  579.    else k:=value(s);
  580.    if neg then k:=0-k;
  581.   end;
  582.  str2int:=k;
  583. end;
  584.  
  585. function distance(x1,y1,x2,y2:real):real;
  586. begin
  587.  distance:=abs(sqrt(sqr(y1-y2)+sqr(x1-x2)));
  588. end;
  589.  
  590. function find_angle(xx,yy,tx,ty:real):real;
  591. var
  592.  i,j,k,v,z:integer;
  593.  q:real;
  594. begin
  595.  q:=0;
  596.     v:=abs(round(tx-xx));
  597.     if v=0 then      {v:=0.001;}
  598.      begin
  599.       if (tx=xx) and (ty>yy) then q:=pi;
  600.       if (tx=xx) and (ty<yy) then q:=0;
  601.      end
  602.     else
  603.      begin
  604.       z:=abs(round(ty-yy));
  605.       q:=abs(arctan(z/v));
  606.       if (tx>xx) and (ty>yy) then q:=pi/2+q;
  607.       if (tx>xx) and (ty<yy) then q:=pi/2-q;
  608.       if (tx<xx) and (ty<yy) then q:=pi+pi/2+q;
  609.       if (tx<xx) and (ty>yy) then q:=pi+pi/2-q;
  610.       if (tx=xx) and (ty>yy) then q:=pi/2;
  611.       if (tx=xx) and (ty<yy) then q:=0;
  612.       if (tx<xx) and (ty=yy) then q:=pi+pi/2;
  613.       if (tx>xx) and (ty=yy) then q:=pi/2;
  614.      end;
  615.  find_angle:=q;
  616. end;
  617.  
  618. function find_anglei(xx,yy,tx,ty:real):integer;
  619. var
  620.  i:integer;
  621. begin
  622.  i:=round(find_angle(xx,yy,tx,ty)/pi*128+256);
  623.  while (i<0) do
  624.    inc(i,256);
  625.  i:=i and 255;
  626.  find_anglei:=i;
  627. end;
  628.  
  629. {FIFI}
  630. function bin(n:integer):string;
  631. var
  632.  i:integer;
  633.  bin_string:string;
  634. begin
  635.  bin_string:='';
  636.  for i:=0 to 15 do
  637.   begin
  638.    if (n mod 2) = 0 then
  639.     bin_string:= '0' + bin_string
  640.    else bin_string:= '1' + bin_string;
  641.    n:=n div 2;
  642.   end;
  643.  bin:=bin_string;
  644. end;
  645. {/FIFI}
  646.  
  647. {FIFI}
  648. function decimal(num,length:integer):string;
  649.    {this can also be acheived by zero_pad(num,length);}
  650. var
  651.  dec_string:string;
  652.  i:integer;
  653. begin
  654.  dec_string:='';
  655.  for i:=1 to length do
  656.   begin
  657.    dec_string:=chr((num mod 10)+48) + dec_string;
  658.    num:=num div 10;
  659.   end;
  660.  decimal:=dec_string;
  661. end;
  662. {/FIFI}
  663.  
  664. end.